<template>
  <div class="sub-sort">
    <div class="sort">
      <a href="javascript:;" :class="{active: sortParams.sortField === null}" @click="changeSort(null)">默认排序</a>
      <a href="javascript:;" :class="{active: sortParams.sortField === 'publishTime'}" @click="changeSort('publishTime')">最新商品</a>
      <a href="javascript:;" :class="{active: sortParams.sortField === 'orderNum'}" @click="changeSort('orderNum')">最高人气</a>
      <a href="javascript:;" :class="{active: sortParams.sortField === 'evaluateNum'}" @click="changeSort('evaluateNum')">评论最多</a>
      <a href="javascript:;" @click="changeSort('price')" >
        商品价格
        <i class="arrow up" :class="{active: sortParams.sortField === 'price' && sortParams.sortMethod === 'desc'}"></i>
        <i class="arrow down" :class="{active: sortParams.sortField === 'price' && sortParams.sortMethod === 'asc'}"></i>
      </a>
    </div>
    <div class="check">
      <XtxCheckbox @change="changeCheck" v-model="sortParams.inventory">仅显示有货商品</XtxCheckbox>
      <XtxCheckbox @change="changeCheck" v-model="sortParams.onlyDiscount">仅显示特惠商品</XtxCheckbox>
    </div>
  </div>
</template>

<script>
import { reactive } from 'vue'
export default {
  name: 'SubSort',
  components: {
  },
  setup (props, { emit }) {
    const sortParams = reactive({
      inventory: false, // 显示有货商品开关
      onlyDiscount: false, // 显示特惠商品开关
      sortField: null, // 排序字段
      sortMethod: null // 排序规则
    })

    // 改变排序
    const changeSort = (sortField) => {
      // 判断是否点击价格排序,是的话执行以下代码
      if (sortField === 'price') {
        // 将price值重新赋值 使价格排序的商家图标点亮
        sortParams.sortField = sortField

        // 判断排序规则是否为空,是的话将sortMethod值初始为'desc'
        if (sortParams.sortMethod === null) {
          sortParams.sortMethod = 'desc'
        } else { // 否则的话将值取反,则为asc
          sortParams.sortMethod = sortParams.sortMethod === 'desc' ? 'asc' : 'desc'
        }
      } else {
        // 如果筛选规则未改变的话,则不执行变动,停止逻辑
        if (sortParams.sortField === sortField) return

        // 如果筛选规则改变的话,执行以下代码
        sortParams.sortField = sortField
        sortParams.sortMethod = null
      }
      emit('sort-change', sortParams)
    }
    const changeCheck = () => {
      emit('sort-change', sortParams)
    }

    return { changeSort, sortParams, changeCheck }
  }
}
</script>

<style lang="less" scoped>
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>
